﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;


  public static  class AESExample
    {
        static string key = "DomLSdHJSkNDfGmt1seX3FyfI446LurB"; // AES-256要求32字节的密钥  
        static string iv = "4AibWt7HBvrlEDup"; // AES要求16字节的初始化向量  
        //private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public static string EncryptString(string plainText)
        {
            //if (plainText == null || plainText.Length <= 0)
            //    logger.Debug($"加密文件长度不对"); 
            //if (key == null || key.Length != 32) // AES-256要求32字节的密钥  
            //    logger.Debug($"加密32字节的密钥问题");
            //if (iv == null || iv.Length != 16) // AES要求16字节的初始化向量  
            //    logger.Debug($"加密初始化向量问题"); 

            byte[] data = Encoding.UTF8.GetBytes(plainText);
            using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV = Encoding.UTF8.GetBytes(iv);
                aes.Mode = CipherMode.CBC; // 使用CBC模式，需要初始化向量（IV）  
                aes.Padding = PaddingMode.PKCS7; // 使用PKCS7填充模式，可提供数据完整性校验功能（虽然不是强制要求）  
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
        }

        public static string DecryptString(string cipherText)
        {
            //if (cipherText == null || cipherText.Length <= 0)
            //    logger.Debug($"解密文件长度不对");
            //if (key == null || key.Length != 32) // AES-256要求32字节的密钥  
            //    logger.Debug($"解密32字节的密钥问题");
            //if (iv == null || iv.Length != 16) // AES要求16字节的初始化向量  
            //    logger.Debug($"解密初始化向量问题");

            byte[] data = Convert.FromBase64String(cipherText);
            using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
            {
                aes.Key = Encoding.UTF8.GetBytes(key);
                aes.IV = Encoding.UTF8.GetBytes(iv);
                aes.Mode = CipherMode.CBC; // 使用CBC模式，需要初始化向量（IV）  
                aes.Padding = PaddingMode.PKCS7; // 使用PKCS7填充模式，可提供数据完整性校验功能（虽然不是强制要求）  
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(data, 0, data.Length);
                        cs.FlushFinalBlock();
                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
        }

    }

